package com.ft.oa.service.technologyService.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.Attachment;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.domain.technology.TechnologyDoc;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.mapper.attachment.AttachmentMapper;
import com.ft.oa.mapper.technologyMapper.ItechnologyDocMapp;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.service.technologyService.ItechnologyDocService;
import com.ft.oa.utils.SecurityUtils;
import com.ft.oa.utils.UploadFileUtile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class TechnologyDocServiceImpl implements ItechnologyDocService {
    @Resource
    private ItechnologyDocMapp technologyDocMapp;
    @Resource
    private DirectoryService directoryService;
    @Override
    public List<TechnologyDoc> selectjsparticulars(Long jsid) {
        //行业详情id
        QueryWrapper<TechnologyDoc> wrapper = new QueryWrapper();
        wrapper.eq("technology_id",jsid);
        return technologyDocMapp.selectList(wrapper);
    }

    @Override
    public int docvaluenum(Long jsid, int valueid) {
        //行业详情id
        QueryWrapper<TechnologyDoc> wrapper = new QueryWrapper();
        wrapper.eq("technology_id",jsid);
        wrapper.eq("valueid",valueid);
        return technologyDocMapp.selectCount(wrapper);
    }

    /**
     * 查询技术列表详情
     * @param industryId
     * @param lvalueid
     * @param lfileName
     * @param page
     * @param size
     * @return
     */
    @Override
    public  Page<TechnologyDoc> recordList(String industryId, String lvalueid, String lfileName, int page, int size) {
        QueryWrapper<TechnologyDoc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("technology_id",industryId);
        queryWrapper.eq("delltech","0");
        if (!StringUtils.isEmpty(lvalueid)){
            queryWrapper.eq("valueid",lvalueid);
        }
        if(!StringUtils.isEmpty(lfileName)){
            queryWrapper.eq("doc_name",lfileName);
        }
        Page<TechnologyDoc> pageIndustrytion = new Page<>(page, size);
        Page<TechnologyDoc> IndustryPage = technologyDocMapp.selectPage(pageIndustrytion, queryWrapper);
        List<TechnologyDoc> industryDocs = IndustryPage.getRecords();
        industryDocs.forEach(industryDoc ->{
            String achment = industryDoc.getAchment();
            industryDoc.setAchment("");
            industryDoc.setAttachments(JSONObject.parseArray(achment, AttachmentDto.class));
        });
        return IndustryPage;
    }

    /**
     * 新增技术详情
     * @param itechnologyDoc
     * @return
     */
    @Override
    public int addTechnicaldetails(TechnologyDoc itechnologyDoc) {
        List<AttachmentDto> attachments = itechnologyDoc.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            itechnologyDoc.setAchment(json);
        }else{
            itechnologyDoc.setAchment(null);
        }
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        // 获取当前用户名称用作展示售前项目管理
        Long creator = currentUser.getId();
        //创建人
        itechnologyDoc.setCreator(creator);
        int insert = technologyDocMapp.insert(itechnologyDoc);
        String valueid = Integer.toString(itechnologyDoc.getValueid());
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.FILING_ITF_TFA.getCode(),String.valueOf(itechnologyDoc.getId()),valueid,"");
        return insert;
    }

    /**
     * 删除技术详情
     * @param technologyId
     * @return
     */
    @Override
    public int dellTechnicaldatails(int technologyId) {
        TechnologyDoc technologyDoc= new TechnologyDoc();
        technologyDoc.setDelltech("1");
        QueryWrapper<TechnologyDoc> wrapper = new QueryWrapper<>();
        wrapper.eq("id",technologyId);
        directoryService.removeAttachment(DirectoryTypeEnum.FILING_ITF_TFA.getCode(),String.valueOf(technologyId));
        return technologyDocMapp.update(technologyDoc, wrapper);
    }

    /**
     * 修改技术详情
     * @param itechnologyDoc
     * @return
     */
    @Override
    public int updataTachnicaldetails(TechnologyDoc itechnologyDoc) {
        QueryWrapper<TechnologyDoc> wrapper = new QueryWrapper<>();
        wrapper.eq("id",itechnologyDoc.getId());
        List<AttachmentDto> attachments = itechnologyDoc.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            itechnologyDoc.setAchment(json);
        }else{
            itechnologyDoc.setAchment(null);
        }
        String valueid = Integer.toString(itechnologyDoc.getValueid());
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.FILING_ITF_TFA.getCode(),String.valueOf(itechnologyDoc.getId()),valueid,"");
        return technologyDocMapp.update(itechnologyDoc, wrapper);
    }
}
